package com.bitcat.base.core

import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.viewModelScope
import androidx.viewbinding.ViewBinding
import com.bitcat.base.ext.dismissLoadingExt
import com.bitcat.base.ext.initViewBinding
import com.bitcat.base.ext.initViewModel
import com.bitcat.base.ext.showLoadingExt
import com.bitcat.base.net.LoadingDialogEntity
import com.bitcat.base.net.LoadingType
import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType

abstract class BaseActivity<VM : BaseViewModel, VB : ViewBinding> : AppCompatActivity(), IBaseView {

    lateinit var viewModel: VM

    var _binding: VB? = null
    val binding get() = _binding!!

//    abstract val viewModel: VM


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        _binding = initViewBinding()
        setContentView(binding.root)

        viewModel = initViewModel()
        observeUiChange()
        initView()

    }

    private fun observeUiChange() {


        viewModel.loadingChange.run {
            //加载中
            loading.observe(this@BaseActivity) {
                when (it.loadingType) {
                    LoadingType.LOADING_NULL -> {}

                    LoadingType.LOADING_DIALOG -> {
                        showOrDismissLoading(it)
                    }

                    LoadingType.LOADING_XML -> {
//                        showLoading(it.loadingMessage)
                    }

                    LoadingType.LOADING_CUSTOM -> {
//                        showCustomLoading(it.loadingMessage)
                    }

                    else -> {
                    }
                }
            }
        }
    }


    override fun showOrDismissLoading(it: LoadingDialogEntity) {
        if (it.isShow){
            //显示弹窗
            showLoadingExt(it.loadingMessage,viewModel.viewModelScope)
        } else {
            //关闭弹窗
            dismissLoadingExt()
        }

    }




    abstract fun initView()


    override fun onDestroy() {
        _binding = null
        super.onDestroy()
    }


}